/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lct.eq2.backup;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 *
 * @author Me
 */
public class TestClient1 {
    // Creates a non-blocking socket channel for the specified host name and port.
    // connect() is called on the new channel before it is returned.
    public static SocketChannel createSocketChannel(String hostName, int port) throws IOException {
        // Create a non-blocking socket channel
        SocketChannel sChannel = SocketChannel.open();
        sChannel.configureBlocking(false);

        // Send a connection request to the server; this method is non-blocking
        sChannel.connect(new InetSocketAddress(hostName, port));
        return sChannel;
    }

    public static void main(String args[]) {
        // Create a non-blocking socket and check for connections
        try {
            // Create a non-blocking socket channel on port 80
            SocketChannel sChannel = createSocketChannel("kris", 13267);

            // Before the socket is usable, the connection must be completed
            // by calling finishConnect(), which is non-blocking
            while (!sChannel.finishConnect()) {
            // Do something else
            }

            ByteBuffer buf = ByteBuffer.wrap("Testing!".getBytes());
            buf.flip();
            sChannel.write(buf);
            buf.compact();

        // Socket channel is now ready to use
        } catch (IOException e) {
        }
    }
}
